<#include "/template/_ListPageWithTree.html" /> 
<#assign left_title="部门列表"> 
<#assign center_title="用户角色关系">
<@left>
<ul id="lefttree" class="easyui-tree" url="${root}/framedept/leftTree"></ul>
<script type="text/javascript">
	$(function() {
		$('#lefttree').tree({
			onClick : function(node) {
				$('#dg').datagrid({
					url : '${root}/userrolerelation/table?code=' + node.attributes.deptCode
				});
			}
		});
	});
</script>
</@left> <@center>
<div id="tb" style="height: auto">

	<div >
		<form id="fm" method="get" action="${root}/userrolerelation/search">
			角色类别: <select id="roleType" name="roleTypeId"
				style="width:120px"
				data-options="valueField:'id',  
                    textField:'roleTypeName'"></select>
             <a href="#" class="easyui-linkbutton"
				onclick="submitForm();"
				data-options="plain:true,iconCls:'icon-search'">搜索</a>
			
			  <a href="#" class="easyui-linkbutton"
				onclick="saveUserRole();"
				data-options="plain:true,iconCls:'icon-save'">保存角色配置</a>
		</form>
	</div>
</div>
				<table id="dg" toolbar="#tb">
					<thead>
						<tr>
							<th data-options="field:'displayname'">用户名</th> 
							<#list acc as a>
							  <th data-options="field:'${a.id}',formatter:check,align:'center'">${a.roleName}</th>
							</#list>
							<th data-options="field:'id',hidden:true"></th>
						</tr>
					</thead>
				</table>


<script type="text/javascript">

$(function(){
	$('#roleType').combobox({
        url:'${root}/framerole/getRoleType',
        panelHeight:'auto'  
     });
	
	$('#roleType').combobox('setValue', '${typeId!}');
	
	$('#dg').datagrid({  
	    url:'${root}/userrolerelation/table?typeId=${typeId!}',
	    rownumbers:true,
	    pagination:true,
	    pageSize : ${tableSize!},
	    onDblClickRow:function(){
	    	editRoleType();
		},
		fit:true,
		checkbox:true,
		singleSelect:true,
		selectOnCheck:false
	});
});

function check(val,row){
	var select =false;
	var role = "";
	if(val.indexOf("@")==0){
		select = true;
		role = val.substring(1,val.length);		
	}else{
		role = val;
	}
	return '<input type="checkbox" '+(select?'checked':'')+' value="'+role+':'+row.id+'" name="user-role"></input>';
}

function submitForm(){
	$('#fm').form('submit',{success:function(url){
		window.location=url;
	}});
}

function saveUserRole(){
	var str="";
	var list = document.getElementsByName("user-role");
	for(var i=0;i<list.length;i++){
		if(list[i].checked){
			str+=list[i].value+",";
		}
	}

	var data = $('#dg').datagrid('getData').rows;
	var ids = "";
	for(var i in data){
		ids+=data[i].id+",";
	}
	$.ajax({
		url:"${root}/userrolerelation/saveUserRoleGroup",
		type:"POST",
		data:{
			userRoles:str,
			idlist:ids
		},
		success:function(msg){
			Alert.info(msg,"系统消息");
		}
	});
}

</script>
</@center>
